﻿using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace shopassient
{
   public  class CSV
    {
       private int liesum = 0;
       private int hangsum = 0;
       private string[] lie;
       private ArrayList rowAL;        //行链表,CSV文件的每一行就是一个链
       private string fileName;       //文件名

       private Encoding encoding; 
         public CSV(string fileName)
        {
            this.rowAL = new ArrayList();
            this.fileName = fileName;
            this.encoding = Encoding.Default;
            LoadCsvFile();
        }
         /// <summary>
         /// 载入CSV文件
         /// </summary>
         private void LoadCsvFile()
         {
             //对数据的有效性进行验证

             if (this.fileName == null)
             {
                 throw new Exception("请指定要载入的CSV文件名");
             }
             else if (!File.Exists(this.fileName))
             {
                 throw new Exception("指定的CSV文件不存在");
             }
             else
             {
             }
             if (this.encoding == null)
             {
                 this.encoding = Encoding.Default;
             }

             StreamReader sr = new StreamReader(this.fileName, this.encoding);
             string fileDataLine;
             //while (true)
             //{

             fileDataLine = sr.ReadToEnd();
             fileDataLine = fileDataLine.Replace(">\r\n<", "><");
                 string[] tmp = fileDataLine.Split(new char[] { '\n' },StringSplitOptions.RemoveEmptyEntries);
                 if (fileDataLine == null)
                 {
                     //break;
                     return;
                 }
                 foreach (string s in tmp)
                 {
                     //rowAL.Add(fileDataLine);
                     rowAL.Add(s.Trim('\r'));
                 }
             //}
             sr.Close();
         }

         public DataTable readTable()
         {
             hangsum = rowAL.Count;
             DataTable dt = new DataTable();
             char split = ' ';
             if (rowAL[0].ToString().IndexOf("\t") > 0)
                 split = '\t';
             else
                 if (rowAL[0].ToString().IndexOf(",") > 0)
                     split = ',';
             //表头列
             lie = rowAL[0].ToString().Split(split);
             liesum = lie.Length;
             for (int i = 0; i < liesum; i++)
             {
                 if (lie[liesum-1] == "") liesum = liesum - 1;
                 DataColumn dc = new DataColumn();
                 dc.AllowDBNull = true;
                 dc.Caption = lie[i].Trim(new char[] { '"' });
                 dc.ColumnName = lie[i].Trim(new char[] { '"' });
                // dc.DataType = Type.GetType("string");
                 dt.Columns.Add(dc);
             }

             //内容
             for (int i = 1; i < hangsum; i++)
             {
                 lie = rowAL[i].ToString().Split(new char[] { split });
                 liesum = lie.Length;
                 if (lie[liesum-1] == "") liesum = liesum - 1;

                 DataRow dr = dt.NewRow();
                 for (int j = 0; j < liesum; j++)
                 {
                     string temp = lie[j].Trim(new char[] { '"' });
                     if (temp == "0")
                         dr[j] = "否";
                     else
                         if (temp == "1")
                             dr[j] = "是";
                         else
                             if (temp == "a")
                                 dr[j] = "拍卖";
                             else if (temp == "b")
                                 dr[j] = "一口价";
                             else
                                 dr[j] = temp;

                 }
                 dt.Rows.Add(dr);
             }
             return dt;
         }
    }
}
